home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6025 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  88 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Using char array in functions
  5. Date: 21 Feb 1996 14:44:33 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gg78hINNg0j@keats.ugrad.cs.ubc.ca>
  8. References: <20FEB199609155775@sundog.caltech.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <20FEB199609155775@sundog.caltech.edu>,
  12. STUART F. TAYLOR (818)395-3807, MC 264-33 CALTECH PASADENA CA 91125 <taylor@sundog.caltech.edu> wrote:
  13. >I've searched the faq's and postings, but still am looking for a clear 
  14. >explanation on how to use arrays of char strings in functions, especially when 
  15. >I want to have the function modify the array.  I find I can't just pass the 
  16. >name of the string as a pointer as I do for single strings.
  17.  
  18. An array of strings is basically an array of character pointer objects, which
  19. hold the addresses of (named or unnamed) zero-terminated character arrays.
  20.  
  21. You can directly pass an array of strings to a function, in which you can
  22. modify or examine any of the pointers. 
  23.  
  24.     Here is a piece of trivia:  A data structure consisting of a
  25.     vector of vector ``descriptors'' is called an ``Iliffe vector'',
  26.     after its inventor, John Iliffe.  In this case, the descriptors
  27.     are pointers, and vectors are basically arrays.
  28.  
  29. Here is how you declare a string Iliffe vector, which contains pointers to
  30. static, unnamed literal strings:
  31.  
  32.     char *strarray[] = { "Foo", "Bar" };
  33.  
  34. This means that ``strarray'' is an array of objects of type ``char *''.
  35. There is a missing size, so the compiler will obtain the size by counting the
  36. number of initializers. The strarray[0] will hold string ``Foo'' by storing a
  37. pointer to it, and strarray[1] will hold string ``Bar''.
  38.  
  39. The C language does not specify the conventions for representing the size of a
  40. string array. One way you can do it is by adopting a convention, throughout
  41. your program, that the last element of a string array will be a null pointer:
  42.  
  43.     char *strarray[] = { "Foo", "Bar", 0 };
  44.  
  45. That allows any function to find the end of your Iliffe Vector just by looking
  46. for a null pointer. You can also get the size of such an array using
  47. sizeof(strarray), but this does not work inside a function. A function which
  48. receives an array of strings can be defined like this:
  49.  
  50.     int strarylen(char *array[])
  51.     {
  52.         /* look for the null pointer, and report array size    */
  53.         int i;
  54.         for (i = 0; array[i] != 0; i++)
  55.             ;
  56.         return i;
  57.     }
  58.  
  59. Alternatively, the "char *array[]" parameter can be declared as "char **array".
  60. In the declarations of function parameters, the two are considered equivalent:
  61.  
  62.     If a parameter is declared to have type ``array of <type>,''
  63.     the declaration is adjusted to read ``pointer to <type>;''
  64.     (K&R2 A10.1)
  65.  
  66. Either way, the size of the referenced array is not known inside the function,
  67. so you rely on the presence of the zero pointer at the end, or you have to pass
  68. the size around as an extra argument. There is no dictated convention; the
  69. representation you choose is up to you.
  70.  
  71. You could, for instance, define a structure which holds arbitrary string arrays
  72. (via a pointer to the first char * in the array, and also has the length:
  73.  
  74.     struct striliffe {
  75.         char **vector;
  76.         int size;
  77.     };
  78.  
  79. Once you start doing heavy string manipulation, you will probably start using
  80. dynamic allocation, and will have to worry about where strings come from.
  81.  
  82. It's possible to have an array of strings, some of which are static, modifiable
  83. arrays, some of which are unmodifiable string literals and others which come
  84. from dynamic storage!  You can't tell them apart---they are all pointers to
  85. zero-terminated character arrays. Try to avoid causing a mess like that!
  86. -- 
  87.  
  88.